package com.foodost.idiomer;

import java.util.List;

import net.youmi.android.AdManager;
import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;
import net.youmi.android.banner.AdViewListener;
import net.youmi.android.spot.SpotManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class CavilOneActivity extends Activity implements OnClickListener{
    
    private static final String TAG = CavilOneActivity.class.getSimpleName();
    private TextView wordtv1,wordtv2,wordtv3,wordtv4,wordno;
    private TextView countNum;
    private List<IdiomData> idiomDataList;
    private int index = 0;
    private List<String> idioms;
    private IdiomData idiomData;
    private int grage;
    private LifeImageView lifeImage;
    private int lifeNum = 3;
    private IdiomModel mIdiomModel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cavilone_activity);
        mIdiomModel = new IdiomModel(this);
        initData();
        initView();
        initAds();
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
    	super.onNewIntent(intent);
    	int grage = intent.getIntExtra(Constants.GRAGE_INTENT, 1);
    	if(this.grage != grage){
    		initData();
    		index = 0;
    		idiomData = idiomDataList.get(index);
    		wordtv1.setEnabled(true);
            wordtv2.setEnabled(true);
            wordtv3.setEnabled(true);
            wordtv4.setEnabled(true);
            wordno.setEnabled(true);
    		showIdiomData(idiomData);
    		countNum.setText(String.valueOf(index));
    	}
    }
    
  //从数据库获取数据
    public void initData(){
    	grage = Utils.getOrmGrage(this);
        idiomDataList = mIdiomModel.getIdiomDatas(this, grage, 0);
    }
    
    private void initView(){
        wordtv1  = (TextView)findViewById(R.id.word_1);
        wordtv2  = (TextView)findViewById(R.id.word_2);
        wordtv3  = (TextView)findViewById(R.id.word_3);
        wordtv4  = (TextView)findViewById(R.id.word_4);
        wordno   = (TextView)findViewById(R.id.cavil_no);
        countNum = (TextView)findViewById(R.id.count);
        lifeImage = (LifeImageView)findViewById(R.id.lifes);
        lifeNum = lifeImage.getAllLifes();
        idiomData = idiomDataList.get(index);
        showIdiomData(idiomData);
        wordtv1.setOnClickListener(this);
        wordtv2.setOnClickListener(this);
        wordtv3.setOnClickListener(this);
        wordtv4.setOnClickListener(this);
        wordno.setOnClickListener(this);
        
        AdView adView = new AdView(this,AdSize.FIT_SCREEN);
        LinearLayout adLayout = (LinearLayout)findViewById(R.id.adLayout);
        adLayout.addView(adView);
        adView.setAdListener(adViewLsn);
    }
    
private AdViewListener adViewLsn = new AdViewListener() {
        
        @Override
        public void onSwitchedAd(AdView arg0) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public void onReceivedAd(AdView arg0) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public void onFailedToReceivedAd(AdView arg0) {
            // TODO Auto-generated method stub
            
        }
    };
    
    public void initAds(){
        SpotManager.getInstance(this).loadSpotAds();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG, "onOptionsItemSelected = " + item.getItemId());
        /*if(item.getItemId() == R.id.action_share){
            Log.d(TAG, "R.id.action_share");
        }else */if(item.getItemId() == R.id.action_reset){//重启此activity
            this.recreate();
        }else if(item.getItemId() == R.id.action_settings){
        	Intent in = new Intent();
        	in.setClass(this, Settings.class);
        	startActivity(in);
        }
        return super.onOptionsItemSelected(item);
    }
    
   
    
    @Override
    protected void onDestroy() {
        SpotManager.getInstance(this)
        .unregisterSceenReceiver();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        boolean flag = false;
        if(v.getId() == R.id.word_1){
            Idiom idiomRight = IdiomToCharArr(idiomData.getIdiomRight());
            flag = !wordtv1.getText().toString().equals(String.valueOf(idiomRight.getWord1()));
            setWordTextViewBgAndCount(v,flag);
        }else if(v.getId() == R.id.word_2){
            Idiom idiomRight = IdiomToCharArr(idiomData.getIdiomRight());
            flag = !wordtv2.getText().toString().equals(String.valueOf(idiomRight.getWord2()));
            setWordTextViewBgAndCount(v,flag);
        }else if(v.getId() == R.id.word_3){
            Idiom idiomRight = IdiomToCharArr(idiomData.getIdiomRight());
            flag = !wordtv3.getText().toString().equals(String.valueOf(idiomRight.getWord3()));
            setWordTextViewBgAndCount(v,flag);
        }else if(v.getId() == R.id.word_4){
            Idiom idiomRight = IdiomToCharArr(idiomData.getIdiomRight());
            flag = !wordtv4.getText().toString().equals(String.valueOf(idiomRight.getWord4()));
            setWordTextViewBgAndCount(v,flag);
        }else if(v.getId() == R.id.cavil_no){
            flag = idiomData.getIdiomRight().equals(getIdiomOne(idioms));
            setWordTextViewBgAndCount(v,flag);
        }
        
        
    }
    
    public void setWordTextViewBgAndCount(View v,boolean flag){
        if(flag){
            v.setBackgroundResource(R.drawable.bg_pressed_right);
            countNum.setText(String.valueOf(index+1));
        }else{
            v.setBackgroundResource(R.drawable.bg_pressed_error);
            if(lifeNum <=1){
                gameOver(R.string.idiom_failed); 
            }
            lifeNum--;
            lifeImage.setLifes(lifeNum);
            lifeImage.invalidate();
        }
        if(flag){
            wordtv1.startAnimation(setIdiomViewAnimation(wordtv1));
            wordtv2.startAnimation(setIdiomViewAnimation(wordtv2));
            wordtv3.startAnimation(setIdiomViewAnimation(wordtv3));
            wordtv4.startAnimation(setIdiomViewAnimation(wordtv4));
        }
    }
    
  //成语TextView动画
    public Animation setIdiomViewAnimation(View v){
        final View mV = v;
        float x = v.getPivotX();
        float y = v.getPivotY();
        int width = v.getWidth();
        int height = v.getHeight();
        RotateAnimation rAnim = new RotateAnimation(0, 360,width/2 + x,height/2 + y);
        rAnim.setDuration(500);
        AnimationSet animSet = new AnimationSet(false);
        animSet.addAnimation(rAnim);
        animSet.setAnimationListener(new Animation.AnimationListener() {
            
            @Override
            public void onAnimationStart(Animation animation) {
                
            }
            
            @Override
            public void onAnimationRepeat(Animation animation) {
                
            }
            
            @Override
            public void onAnimationEnd(Animation animation) {
                if(mV.getId() == R.id.word_1){
                    resetView();
                }
                
            }
        });
        return animSet;
    }
    
    //更新成语TextView和拼音内容
    public void resetView(){
        getNextIdiomData();
        if(idiomData != null){
            showIdiomData(idiomData);
            wordtv1.setBackgroundResource(R.drawable.bgselector);
            wordtv2.setBackgroundResource(R.drawable.bgselector); 
            wordtv3.setBackgroundResource(R.drawable.bgselector);
            wordtv4.setBackgroundResource(R.drawable.bgselector);
            wordno.setBackgroundResource(R.drawable.bgselector);
            
        }else{
            wordtv1.setBackgroundResource(R.drawable.bgselector);
            wordtv2.setBackgroundResource(R.drawable.bgselector); 
            wordtv3.setBackgroundResource(R.drawable.bgselector);
            wordtv4.setBackgroundResource(R.drawable.bgselector);
            wordno.setBackgroundResource(R.drawable.bgselector);
            gameOver(R.string.idiom_end);
        }
    }
    
    public void gameOver(int strRedId){
        wordtv1.setEnabled(false);
        wordtv2.setEnabled(false);
        wordtv3.setEnabled(false);
        wordtv4.setEnabled(false);
        wordno.setEnabled(false);
        Toast.makeText(this, strRedId, Toast.LENGTH_LONG).show();
    }
    
  //获取到下一个进入的成语
    public IdiomData getNextIdiomData(){
        if(idiomDataList.size() -1> index){
            idiomData = idiomDataList.get(++index);
        }else{
            idiomData = null;
        }
        return idiomData;
    }

    //得到打乱后的成语列表
    public List<String> getIdioms(IdiomData idiomData) {
        return mIdiomModel.mixIdiom(idiomData);
    }

    public IdiomData getIdiomData() {
        return idiomData;
    }
    
    public String getIdiomOne(List<String> idioms){
        if(idioms.size() > 0){
            return idioms.get(0);
        }
        return getIdiomOne(getIdioms(getNextIdiomData()));
    }
    
    public Idiom IdiomToCharArr(String idiom){
        return new Idiom(idiom);
    }
    
    public void fillWordToTextView(Idiom words){
        if(words.hasData()){
            wordtv1.setText(String.valueOf(words.getWord1()));
            wordtv2.setText(String.valueOf(words.getWord2()));
            wordtv3.setText(String.valueOf(words.getWord3()));
            wordtv4.setText(String.valueOf(words.getWord4()));
        }
    }
    
    public void showIdiomData(IdiomData idiomData){
        idioms = getIdioms(idiomData);
        Idiom words = IdiomToCharArr(getIdiomOne(idioms));
        fillWordToTextView(words);
    }
    


}
